// RedJey Graphics User Interface Library for J2ME Platform.
// Copyright (C) 2008 Yevgen Krapiva
//
// This library is free software; you can redistribute
// it and/or modify it under the terms of the
// GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3.0
// of the License, or any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

package com.incrediblesoft.redjey;

/**
 * @author Yevgen Krapiva<br>
 *         Homepage: http://code.google.com/p/redjey/<br>
 *         Feedback: ykrapiva@gmail.com<br>
 *         Copyright (C) 2008 Yevgen Krapiva<br>
 */

import javax.microedition.lcdui.Graphics;
import java.util.Hashtable;

public class Button extends TextComponent
{
    /**
     * Icon image
     */
    protected CustomImage image;

    /**
     * Constructs button with a given title
     * @param title title
     */
    public Button(String title)
    {
        this(title, null, null);
    }

    /**
     * Constructs button with a given title and style class
     * @param title title
     * @param styleClassName class
     */
    public Button(String title, String styleClassName)
    {
        this(title, null, styleClassName);
    }

    /**
     * Constructs button with a given title and image
     * @param title title
     * @param image image
     */
    public Button(String title, CustomImage image)
    {
        this(title, image, null);
    }

    /**
     * Constructs button with a given title, image and style class
     * @param title title
     * @param image image
     * @param styleClassName style class
     */
    public Button(String title, CustomImage image, String styleClassName)
    {
        super(title, styleClassName);
        
        if (image != null)
            setImage(image);

        setFocusable(true);
    }

    public String getName()
    {
        return "Button";
    }

    protected Dimension calcPreferredSize(int paddingHorizontal, int paddingVertical, CustomFont font, Hashtable attributes)
    {
        return new Dimension((image != null && text.length() > 0 && font != null ? paddingHorizontal : 0) + (text.length() > 0 && font != null ? font.stringWidth(text) : 0) + (image != null ? image.getWidth() : 0),
                             Math.max(text.length() > 0 && font != null ? font.getHeight() : 0, (image != null ? image.getHeight() : 0)));
    }

    protected void calcContentSize()
    {
        contentSize.width = ((image != null && text.length() > 0 && currentStyle.font != null ? currentStyle.paddingHorizontal : 0) + (text.length() > 0 && currentStyle.font != null ? currentStyle.font.stringWidth(text) : 0) + (image != null ? image.getWidth() : 0));
        contentSize.height = Math.max(text.length() > 0 && currentStyle.font != null ? currentStyle.font.getHeight() : 0, (image != null ? image.getHeight() : 0));
    }
    
    public void setState(int index)
    {
        super.setState(index);

        //System.out.println("Button "+toString()+" setState( "+index+" )");

        CustomImage image = (CustomImage) currentStyle.attributes.get("stick-image");
        if (image != null) setImage(image.clone());
    }

    /**
     * Returns image icon
     * @return image
     */
    public CustomImage getImage()
    {
        return image;
    }

    /**
     * Sets image
     * @param image image
     */
    public void setImage(CustomImage image)
    {
        if (this.image != image)
            invalidate();
        this.image = image;
        if (image != null)
            image.parent = this;
    }

    public void tick(int dt)
    {
        super.tick(dt);

        if (image != null)
            image.tick(dt);
    }
    
    protected void validateComponent()
    {
        //nothing to do
    }

   /* public void paint(Graphics g)
    {
        super.paint(g);
    }

    public void repaint()
    {
        super.repaint();
    }
   */
    protected void paintComponent(Graphics g)
    {
        if (image != null)
        {
            image.paint(g, 0, 0, Graphics.LEFT | Graphics.TOP);
            g.translate(image.getWidth() + currentStyle.paddingHorizontal, currentStyle.font != null && text.length() > 0 ? image.getHeight() - currentStyle.font.getHeight() >> 1 : 0);
        }

        if (text.length() > 0)
            currentStyle.font.drawString(g, text, 0, 0, Graphics.LEFT | Graphics.TOP);
    }
}

